package com.bjb.vr.operation.controller;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.*;
import com.bjb.vr.common.constant.EncryptConstant;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.OptClassifyConfigService;
import com.bjb.vr.common.service.OptFeatureConfigService;
import com.bjb.vr.common.service.OptTopicConfigService;
import com.bjb.vr.common.service.ResContentService;
import com.bjb.vr.common.utils.BeanMapUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @description: 接收客户端请求，返回云端资源数据
 * @author: hujingbo
 * @create: 2022-12-26 14:23
 **/
@RestController
@RequestMapping("/api/operation/v1")
@Slf4j
public class OptCloudResourceController {

    @Resource
    private ResContentService resContentService;

    @Resource
    private OptFeatureConfigService featureConfigService;

    @Resource
    private OptClassifyConfigService classifyConfigService;

    @Resource
    private OptTopicConfigService topicConfigService;

    /**
     * 验证签名
     *
     * @param object
     * @return
     */
    private boolean checkSign(Object object) throws IllegalAccessException {
        byte[] key = EncryptConstant.ENCRYPT_KEY.getBytes();
        HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
        StringBuilder builder = new StringBuilder();
        Map<String, Object> beanToMap = BeanMapUtil.beanToMap(object);
        String tenantId = beanToMap.get("tenantId").toString();
        String requestId = beanToMap.get("requestId").toString();
        String requestTime = beanToMap.get("requestTime").toString();
        String signature = beanToMap.get("signature").toString();
        String dataInfo = builder.append(tenantId).append(requestId).append(requestTime).toString();
        String macHex = mac.digestHex(dataInfo);
        return macHex.equals(signature);
    }

    /**
     * 查询推荐位列表数据
     *
     * @param optFeatureAo
     * @return
     */
    @PostMapping("/cloud/queryFeatureList")
    public BaseResult queryFeatureList(@RequestBody OptFeatureAo optFeatureAo) throws IllegalAccessException {
        // 1、验证签名
        boolean checkSign = checkSign(optFeatureAo);
        if (!checkSign) {
            return CommonResult.Fail("签名验证失败");
        }
        // 2、查询数据
        List<OptFeatureVo> featureVoList = featureConfigService.queryFeatureList(optFeatureAo);
        return CommonResult.Ok().setResult(featureVoList);
    }

    /**
     * 查询资源详情
     *
     * @param resContentAo
     * @return
     */
    @PostMapping("/cloud/queryResourcesOne")
    public BaseResult queryResDetail(@RequestBody ResContentAo resContentAo) throws IllegalAccessException {
        // 1、验证签名
        boolean checkSign = checkSign(resContentAo);
        if (!checkSign) {
            return CommonResult.Fail("签名验证失败");
        }
        // 2、查询数据
        OptContentVo optContentVo = resContentService.queryResourcesOne(resContentAo);
        return CommonResult.Ok().setResult(optContentVo);
    }

    /**
     * 查询分类推荐列表数据
     *
     * @param optClassifyAo
     * @return
     */
    @PostMapping("/cloud/queryClassifyList")
    public BaseResult queryClassifyList(@RequestBody OptClassifyAo optClassifyAo) throws IllegalAccessException {
        // 1、验证签名
        boolean checkSign = checkSign(optClassifyAo);
        if (!checkSign) {
            return CommonResult.Fail("签名验证失败");
        }
        // 2、查询数据
        List<OptClassifyVo> classifyVoList = classifyConfigService.queryClassifyList(optClassifyAo);
        return CommonResult.Ok().setResult(classifyVoList);
    }

    /**
     * 查询资源列表
     *
     * @param resContentAo
     * @return
     */
    @PostMapping("/cloud/queryResourcesList")
    public BaseResult queryResourcesList(@RequestBody ResContentAo resContentAo) throws IllegalAccessException {
        // 1、验证签名
        boolean checkSign = checkSign(resContentAo);
        if (!checkSign) {
            return CommonResult.Fail("签名验证失败");
        }
        // 2、查询数据
        IPage<OptContentVo> resourcesList = resContentService.queryResourcesList(resContentAo);
        return CommonResult.Ok().setResult(resourcesList);
    }

    /**
     * 查询专题推荐列表数据
     *
     * @param optTopicAo
     * @return
     */
    @PostMapping("/cloud/queryTopicList")
    public BaseResult queryTopicList(@RequestBody OptTopicAo optTopicAo) throws IllegalAccessException {
        // 1、验证签名
        boolean checkSign = checkSign(optTopicAo);
        if (!checkSign) {
            return CommonResult.Fail("签名验证失败");
        }
        // 2、查询数据
        List<OptTopicVo> topicVoList = topicConfigService.queryTopicList(optTopicAo);
        return CommonResult.Ok().setResult(topicVoList);
    }

    /**
     * 查询头盔云端资源列表
     *
     * @param resClientCloudAo
     * @return
     */
    @PostMapping("/cloud/queryGlassResourcesList")
    public BaseResult queryGlassResourcesList(@RequestBody ResClientCloudAo resClientCloudAo) throws IllegalAccessException {
        // 1、验证签名
        boolean checkSign = checkSign(resClientCloudAo);
        if (!checkSign) {
            return CommonResult.Fail("签名验证失败");
        }
        // 2、查询数据
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        List<ResClientCloudVo> resourcesList = resContentService.queryGlassResourcesList(resClientCloudAo);
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        return CommonResult.Ok().setResult(resourcesList);
    }
}
